﻿using System;

namespace GIMS.Def.Arguments
{
    [Flags]
    public enum GeometryType
    {
        percent = 0x01,
        xypercent = 0x02,
        width = 0x04,
        height = 0x08,
        widthxheight = 0x10,
        area = 0x20,
        offsetNeeded = 0x40
    }
    public enum GeometryRadio
    {
        none,
        minimal,    // ^ flag
        exact,      // ! flag
        more,       // > flag
        less        // < flag
    }

    public class GeometryArgument : Argument
    {
        private int scale;
        private int scaleX;
        private int scaleY;
        private int width;
        private int height;
        private int area;
        private int xOffset;
        private int yOffset;
        private GeometryRadio geometryRadio;

        private GeometryType supportedGeometryTypes;
        private GeometryType selectedGeometryType;

        public GeometryArgument()
        {
            name = "Geometry Argument";
            type = Flags.ArgumentType.geometry;
            geometryRadio = GeometryRadio.none;
            supportedGeometryTypes = (GeometryType)0x7F;    //all geometryTypes are OK
            selectedGeometryType = GeometryType.widthxheight;  //default geometry type is width X height  
        }

        public GeometryArgument(GeometryType supported, GeometryType selected)
        {
            name = "Geometry Argument";
            type = Flags.ArgumentType.geometry;
            geometryRadio = GeometryRadio.none;
            supportedGeometryTypes = supported;
            selectedGeometryType = selected;
        }

        public GeometryType SupportedGeometryTypes
        {
            get { return supportedGeometryTypes; }
            set
            {
                supportedGeometryTypes = value;
                OnPropertyChanged("SupportedGeometryTypes");
            }
        }

        public GeometryType SelectedGeometryType
        {
            get { return selectedGeometryType; }
            set
            {
                selectedGeometryType = value;
                OnPropertyChanged("SelectedGeometryType");
            }
        }

        public override string CreateScriptPart()
        {
            switch (selectedGeometryType)
            {
                case GeometryType.percent:
                    return Scale + "%%";
                case GeometryType.xypercent:
                    return ScaleX + "%%x" + ScaleY + "%%";
                case GeometryType.width:
                    return Width + "";
                case GeometryType.height:
                    return "x" + Height;
                case GeometryType.widthxheight:
                    return CreateWidthHeightScriptPart();
                case GeometryType.area:
                    return Area + "@";
                case GeometryType.offsetNeeded | GeometryType.widthxheight:
                    return Width + "x" + Height + "+" + XOffset + "+" + YOffset; //TODO +/- für offsets
                default:
                    return "";
            }
        }

        private string CreateWidthHeightScriptPart()
        {
            string command = Width + "x" + Height;
            switch (geometryRadio)
            {
                case GeometryRadio.minimal:
                    command = "\"" + command + "^\"";
                    break;
                case GeometryRadio.exact:
                    command = "\"" + command + "!\"";
                    break;
                case GeometryRadio.more:
                    command = "\"" + command + ">\"";
                    break;
                case GeometryRadio.less:
                    command = "\"" + command + "<\"";
                    break;
            }
            return command;
        }


        public int Scale
        {
            get { return scale; }
            set
            {
                scale = value;
                OnPropertyChanged("geometry");
            }
        }

        public int ScaleX
        {
            get { return scaleX; }
            set
            {
                scaleX = value;
                OnPropertyChanged("geometry");
            }
        }

        public int ScaleY
        {
            get { return scaleY; }
            set
            {
                scaleY = value;
                OnPropertyChanged("geometry");
            }
        }

        public int Width
        {
            get { return width; }
            set
            {
                width = value;
                OnPropertyChanged("geometry");
            }
        }

        public int Height
        {
            get { return height; }
            set
            {
                height = value;
                OnPropertyChanged("height");
            }
        }

        public int Area
        {
            get { return area; }
            set
            {
                area = value;
                OnPropertyChanged("geometry");
            }
        }

        public int XOffset
        {
            get { return xOffset; }
            set
            {
                xOffset = value;
                OnPropertyChanged("geometry");
            }
        }

        public int YOffset
        {
            get { return yOffset; }
            set
            {
                yOffset = value;
                OnPropertyChanged("geometry");
            }
        }

        public GeometryRadio GeometryRadio
        {
            get { return geometryRadio; }
            set
            {
                geometryRadio = value;
                OnPropertyChanged("geometry");
            }
        }
    }
}
